<template>
  <div
    class="nav-bar-box"
    :class="isSideways ? 'sideway-div' : ''"
    :style="{ background: backgroundColor, color: color }"
  >
    <div
      class="status-bar"
      v-if="!isSideways"
      :style="{ height: statusbar + 'px' }"
    ></div>
    <div class="title-content">
      <div
        v-if="!isSideways"
        class="left-item"
        @click="back"
      >
        <span
          class="back-icon"
          v-if="leftIcon === 'arrowleft'"
        ></span>
      </div>
      <div class="middle-item">
        <span>{{ title }}</span>
      </div>
      <div
        class="right-item"
        @click="rightClick"
      >
        <span>{{ rightText }}</span>
      </div>
      <div class="notice-div" v-if="newsBool">
        <img src="@/static/img/notice.png" alt="">
        <span>新消息通知</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'nav-bar',
  props: {
    backgroundColor: {
      type: String,
      default: '#43afa0'
    },
    leftIcon: {
      type: String,
      default: 'arrowleft'
    },
    color: {
      type: String,
      default: '#fff'
    },
    title: {
      type: String,
      default: ''
    },
    rightText: {
      type: String,
      default: ''
    },
    isSideways: {
      type: Boolean,
      default: false
    },
    backTo: Function,
    rightTo: Function
  },
  data () {
    return {

    }
  },
  computed: {
    statusbar () {
      return this.$store.state.screenInfo.statusBarHeight
    },
    newsBool () {
      return this.$store.state.newsBool || false
    }
  },
  methods: {
    back () {
      if (this.backTo) {
        this.backTo()
      } else {
        uni.navigateBack({
          delta: 1,//返回层数，2则上上页
        })
      }
    },
    rightClick () {
      if (this.rightTo) {
        this.rightTo()
      }
    }
  }
}
</script>
<style scoped lang="scss">
.nav-bar-box {
  .status-bar {
    width: 100vw;
  }
  .title-content {
    position: relative;
    width: 100vw;
    height: 44px;
    padding: 0 10px;
    display: flex;
    box-sizing: border-box;
    .left-item {
      width: 60px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .back-icon {
        display: inline-block;
        width: 20rpx;
        height: 20rpx;
        border-bottom: 6rpx solid #fff;
        border-left: 6rpx solid #fff;
        transform: translate(30rpx, 0rpx) rotate(45deg);
      }
    }
    .middle-item {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 40rpx;
    }
    .right-item {
      width: 60px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
    }
    .notice-div {
      height: 100%;
      position: absolute;
      top: 50%;
      left: 20rpx;
      transform: translateY(-50%);
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      img {
        width: 30rpx;
        height: 30rpx;
        margin-right: 10rpx;
      }
    }
  }
}
.nav-bar-box.sideway-div {
  width: 100%;
  height: 100%;
  .title-content {
    width: 100%;
    height: 100%;
    padding: 0;
    flex-direction: column;
    .middle-item {
      width: 200rpx;
      transform: rotate(90deg) translate(-15%, 50rpx);
    }
  }
}
</style>